<template>
	<view class="address">

		<view class="main">
			<view class="list" v-for="(item, index) in initData" :key="index">
				<p class="name" @click="checked(item)">
					<text>{{item.contactUser}}</text> {{item.contactPhone}}
				</p>
				<view class="msg">
					<text class="first" v-if="item.isDefault.value==1" @click="checked(item)">默认</text>
					<view class="text" @click="checked(item)">
						{{item.province}} {{item.city}} {{item.region}} {{item.addrDetail}}
					</view>
					<view class="image">
						<image src="/static/img/edit1.png" @click="edit(item.id)" />
						<image src="/static/img/delete.png" @click="del(item.id)" />
					</view>
				</view>
			</view>
			
			<view v-if="initData.length==0" class="notavailable">
				<image src="../../../static/img/notavailable.png" mode="widthFix"></image>
				<view class="nottext">暂时没有数据</view>
			</view>
			<view class="button" @click="go_url('/pages/shop/my/address_detail')">
				<text>+</text>新增收货地址
			</view>
		</view>
	</view>
</template>

<script>
	import {
		uniIcons
	} from '@dcloudio/uni-ui';
	import address from '../../../servers/my/address.js';
	export default {
		components: {
			uniIcons
		},
		data() {
			return {
				initData: [],
				is_order: false
			}
		},
		
		onShow() {
			this.getDate();
		},
		
		mounted() {
			var _this = this
			var is_order = uni.getStorageSync('address') || this.$route.query.address
			if (is_order == "order") {
				this.is_order = true
			}
		},
		methods: {
			getDate() {
				console.log(12312321312)
				this.initData = []; //数据清空
				address.initData()
					.then(res => {
						console.log(res.data)
						this.initData = res.data;
					})
					.catch(err => {
						console.log(new Error('请检查网络后重试'));
					});
			},
			checked(e) {
				if (this.is_order) {
					uni.$emit('checkedAddress', e,true)
					// var pages = getCurrentPages();
					// var currPage = pages[pages.length - 1]; //当前页面
					// var prevPage = pages[pages.length - 2]; //上一个页面
					// console.log(prevPage,pages)
					// // #ifdef H5 
					// 	prevPage._data.isDoRefresh =  true
					// // #endif
					//  // #ifdef APP-PLUS
					//  prevPage._data.isDoRefresh =  true
					// 	 // prevPage.setData({
					// 	 //        isDoRefresh: true
					// 	 //    })
					//  // #endif
					
					this.commonality.back()
				}
			},
			edit(e) {
				this.commonality.route_baling('/pages/shop/my/address_detail', 'shopmyaddress_detail', e)
			},
			del(id) {
				console.log(12312);
				var _this = this;
				uni.showModal({
					title: '提示',
					content: '你确定要删除该地址吗？',
					success: function(res) {
						if (res.confirm) {
							address.del(id)
								.then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: '删除成功'
										});										
										_this.getDate(); //数据重新刷新
										if(this.initData.length==0){
											uni.$emit('checkedAddress', '')
										}
									} else {
										uni.showToast({
											title: '删除失败，请联系管理员',
											icon:'none'
										});
									}
								})
								.catch(err => {
									console.log(new Error('请检查网络后重试'));
								});

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},

			go_url(url) {
				uni.removeStorageSync('shopmyaddress_detail');
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	.none{
		height: 30vh;
	}
	.address {
		.add {
			background: #fff;
			font-size: 33rpx;
			line-height: 105rpx;
			padding: 0 20rpx;
			border-bottom: 1rpx solid #eeeeee;

			/*no*/
			image {
				position: relative;
				width: 30rpx;
				height: 30rpx;
				top: 2rpx;
				margin-right: 20rpx;
			}

			i {
				float: right;
			}
		}

		.main {
			// position: absolute;
			// bottom: 0;
			// width: 100%;
			// overflow: auto;
			// padding-bottom: 200rpx;
			background: #fff;

			.list {
				padding: 20rpx;
				border-bottom: 1rpx solid #eeeeee;

				/*no*/
				.name {
					font-size: 28rpx;
					margin-bottom: 25rpx;

					text {
						margin-right: 40rpx;
					}
				}

				.msg {
					display: flex;

					.first {
						width: 84rpx;
						height: 40rpx;
						line-height: 40rpx;
						text-align: center;
						background: #d176ae;
						color: #fff;
						border-radius: 10rpx;
						margin-right: 18rpx;
						font-size: 22rpx;
					}

					.text {
						flex: 1;
						font-size: 26rpx;
						color: #666666;
					}

					.image {
						width: 150rpx;

						image {
							display: inline-block;
							width: 39rpx;
							height: 39rpx;
							margin-left: 34rpx;
						}
					}
				}
			}

			.button {
				position: fixed;
				bottom: 72rpx;
				width: -webkit-fill-available;
				margin: 0 20rpx;
				line-height: 90rpx;
				background: linear-gradient(80deg, #d2158a, #fc6395);
				text-align: center;
				color: #fff;
				border-radius: 45rpx;
				font-size: 32rpx;
				box-shadow: 3rpx 0 20rpx rgba(206, 205, 204, 0.35);
			}
		}
	}
</style>
